package com.ititer.helper;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;
import org.springframework.stereotype.Component;

import br.com.caelum.vraptor.validator.I18nMessage;
import br.com.caelum.vraptor.validator.Message;

import com.ititer.enums.PublicKeys;
import com.ititer.web.Bean;

@Component("validationHelper")
public class ValidationHelper {
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Message> validate(final Bean bean) {
		List<Message> messages = new ArrayList<Message>();
		ClassValidator validator = new ClassValidator(bean.getClass());
		InvalidValue[] invalidValues = validator.getInvalidValues(bean);
		for (InvalidValue invalid : invalidValues) {
			//			Message message = new ValidationMessage(invalid.getMessage(), PublicKeys.Error.getValue());
			Message message = new I18nMessage(PublicKeys.Error.getValue(), invalid.getMessage());
			messages.add(message);
		}

		//work for hibernate-validator-4.0.5GA
		//		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		//		Validator validator = factory.getValidator();
		//		Set<ConstraintViolation<Bean>> constraintViolations = validator.validate(bean);
		//		for (ConstraintViolation<Bean> violation : constraintViolations) {
		//			Message message = new ValidationMessage(violation.getMessage(), PublicKeys.Error.getValue());
		//			messages.add(message);
		//		}
		return messages;
	}
}
